package com.chixing.ih.module.programmer.controller;

import com.chixing.ih.json.ResponseData;
import com.chixing.ih.module.programmer.pojo.ProCommunity;
import com.chixing.ih.module.programmer.service.IProCommunityService;
import com.chixing.ih.module.programmer.service.IProgrammerService;
import com.chixing.ih.util.RequestIdUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @version 1.0
 * @Author MasterYan
 * @Date 2020/10/3
 * Time: 12:53
 */
@RestController
@RequestMapping("/proCommunity")
@Api(tags = "程序员模块个人社区相关Api")
public class ProCommunityController {

    @Autowired
    IProCommunityService proCommunityService;

    @Autowired
    IProgrammerService programmerService;

    /**
     * desc:更新个人社区接口
     *
     * @param proCommunity
     * @return
     */
    @ApiOperation(value = "更新个人社区接口", notes = "更新个人社区")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "个人社区id", required = true, dataType = "long"),
            @ApiImplicitParam(name = "githubUrl", value = "github网站", dataType = "string"),
            @ApiImplicitParam(name = "knowUrl", value = "knowUrl网站", dataType = "string"),
            @ApiImplicitParam(name = "stackoverflowUrl", value = "stackoverflowUrl网站", dataType = "string"),
            @ApiImplicitParam(name = "dribbbleUrl", value = "dribbbleUrl网站", dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 201, message = "更新成功"),
            @ApiResponse(code = 400, message = "更新失败"),
    })

    @PutMapping("/proCommunity")
    public Map<String, Object> updateProCommunity(@RequestBody ProCommunity proCommunity, HttpServletRequest request) {
        Long myId = RequestIdUtil.getMyId(request);
        ProCommunity proCommunity1 = new ProCommunity();
        proCommunity.setUserId(myId);
        boolean flag = proCommunityService.updateProCommunity(proCommunity);
        if (flag) {
            programmerService.updateProgrammerTime(myId);
            return ResponseData.REP_CREATED_SUCCESS();
        } else
            return ResponseData.REP_INVALID_ERROR();


    }

    /**
     * desc:查看个人社区接口
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "查看个人社区接口", notes = "查看个人社区")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "long"),

    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
    })

    @GetMapping("/proCommunity/{userId}")
    public Map<String, Object> getProCommunity(@PathVariable("userId") long userId) {
        ProCommunity proCommunity = proCommunityService.getProCommunity(userId);
        return ResponseData.REP_GET_SUCCESS(proCommunity);

    }


}
